//
//  VisitorLoginView.swift
//  weibo2
//
//  Created by xuwzh on 15/10/6.
//  Copyright © 2015年 xuwzh. All rights reserved.
//

import UIKit

///定义代理协议
protocol  VisitorLoginViewDelegate: NSObjectProtocol{
    
    //登录的代理方法
    func visitorLoginViewLoginDidSelected()
    //注册的代理方法
    func visitorLoginViewRegisterDidSelected()

}

class VisitorLoginView: UIView {
    
    @IBOutlet weak var smallIconView: UIImageView!
    
    @IBOutlet weak var iconView: UIImageView!
    
    @IBOutlet weak var messageLabel: UILabel!
    
    //定义代理 ,必须使用weak，避免重复使用，因为没有初始化，所以后面加问号
    
    weak var delegate: VisitorLoginViewDelegate?
    
    
    func setUpInfo(iconImage: String,message : String,isHome :Bool=false){
        
        iconView.hidden = !isHome
        
        if isHome {
            iconView.image=UIImage(named: iconImage)
            
        }else
        {
            smallIconView.image=UIImage(named: iconImage)
        }
        messageLabel.text=message
        
        isHome ? startAnimation() : stopAnimation()
        
    }
    
    /// 启动动画
    func startAnimation(){
        let ani=CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = 2 * M_PI
        ani.repeatCount = MAXFLOAT
        //调整转到的速度
        ani.duration = 15
        smallIconView.layer.addAnimation(ani, forKey: nil)
        
    }
    
    /// 停止动画
    func stopAnimation(){
        
        smallIconView.layer.removeAllAnimations()
    }
    
    //注册
    @IBAction func register() {
        delegate?.visitorLoginViewRegisterDidSelected()
    }
    
    
    //登录
    @IBAction func login() {
        delegate?.visitorLoginViewLoginDidSelected()
    }
}
